//
//  JSLTableBillEditCell.m
//  JingleSpeedLoan
//
//  Created by 宋志彬 on 2018/8/5.
//  Copyright © 2018年 宋质彬. All rights reserved.
//

#import "JSLTableBillEditCell.h"

@interface JSLTableBillEditCell ()<UITextFieldDelegate>
@property (nonatomic , weak) UIView *backView;
@property (nonatomic , weak) UILabel *nameLab;
@property (nonatomic , weak) UITextField *textField;
@property (nonatomic , weak) UILabel *scrLab;
@property (nonatomic , weak) UIImageView *arrowImage;
@property (nonatomic , strong) NSIndexPath *indexPath;

@end

@implementation JSLTableBillEditCell

#pragma mark - init
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor whiteColor];
        
        [self setUpAllChildView];
        
    }
    return self;
}

- (void)configInfo:(NSDictionary *)info indexPath:(NSIndexPath *)indexPath {
    
    self.indexPath = indexPath;
    //    @{ @"name":@"账单名称",
    //       @"contet" : @"",
    //       @"place" :@"请输入（如：买菜）",
    //       @"type":@"text"
    //       },
    
    self.nameLab.text = info[@"name"];
    self.textField.placeholder = info[@"place"];
    self.textField.tag = indexPath.row +1000;
    
    if ([JSLKitFactory isNotNull:info[@"contet"]]) {
        self.scrLab.text = info[@"contet"];
        self.scrLab.textColor = [@"#333333" hexStringToColor];
    }else {
        self.scrLab.text = info[@"place"];
    }
    
    
    
    if ([info[@"type"] isEqualToString:@"text"]) {
        self.scrLab.hidden = YES;
        self.arrowImage.hidden = YES;
        self.textField.hidden = NO;
        
        
    }else if([info[@"type"] isEqualToString:@"select"]){
        self.scrLab.hidden = NO;
        self.arrowImage.hidden = NO;
        self.textField.hidden = YES;
    }else {
        self.scrLab.hidden = NO;
        self.arrowImage.hidden = YES;
        self.textField.hidden = YES;
        
    }
}

- (void)setInfo:(NSDictionary *)info {
    _info = info;

}

#pragma mark - 配置视图
- (void)layoutSubviews{
    [super layoutSubviews];
    [self configFrame];
}

- (void)configFrame{
    
    self.backView.frame = CGRectMake(10*AutoSizeScaleXX, 0*AutoSizeScaleYY, CGRectGetWidth(self.contentView.frame)- 20*AutoSizeScaleXX, 50*AutoSizeScaleYY);
    self.backView.layer.mask = [JSLKitFactory roundedRect:self.backView.bounds cornerRadius:4];
    
    CGSize nameSize = [self.nameLab.text sizeWithAttributes:@{NSFontAttributeName : self.nameLab.font}];
    self.nameLab.frame = CGRectMake( 10 *AutoSizeScaleXX, 0, nameSize.width, self.backView.height);
    
    self.textField.frame = CGRectMake(120*AutoSizeScaleXX, 0, self.backView.width - 120*AutoSizeScaleXX -10*AutoSizeScaleXX, self.backView.height);

    self.arrowImage.frame = CGRectMake(self.backView.width - 15*AutoSizeScaleXX-15*AutoSizeScaleXX,  CGRectGetHeight(self.contentView.frame)*0.5 - 15*AutoSizeScaleYY*0.5, 15*AutoSizeScaleXX, 15*AutoSizeScaleYY);
    
    self.arrowImage.centerY = self.nameLab.centerY;
    
    self.scrLab.frame = CGRectMake(120*AutoSizeScaleXX, 0, self.backView.width - 120*AutoSizeScaleXX -15*AutoSizeScaleXX*2, self.backView.height);
    
}
- (void)setUpAllChildView{
    
    self.backView = [JSLKitFactory jsl_viewWithFrame:CGRectZero backgroundColor:[UIColor whiteColor] superView:self.contentView];

    self.textField = [JSLKitFactory jsl_textFieldWithFrame:CGRectZero WithPlaceholder:nil withDelegat:self superView:self.backView textAlignment:NSTextAlignmentLeft textFont:LIGHT_FONT(14) textColol:[@"#333333" hexStringToColor]];
    [self.textField addTarget:self action:@selector(textfielDidChange:) forControlEvents:UIControlEventEditingChanged];
    self.textField.keyboardType = UIKeyboardTypeDecimalPad;
    self.nameLab = [JSLKitFactory jsl_labelWithFrame:CGRectZero text:nil font:LIGHT_FONT(14) textColor:[@"#333333" hexStringToColor] textAlignment:NSTextAlignmentLeft SuperView:self.backView];
    
    self.arrowImage = [JSLKitFactory jsl_imageViewWithFrame:CGRectZero imageName:@"detial-arrow" superView:self.backView];
    
    self.scrLab = [JSLKitFactory jsl_labelWithFrame:CGRectZero text:nil font:LIGHT_FONT(14) textColor:[@"#cccccc" hexStringToColor] textAlignment:NSTextAlignmentLeft SuperView:self.backView];
}



#pragma mark - action
- (void)textfielDidChange:(UITextField *)textField{
    
    if ([self.delegate respondsToSelector:@selector(inputCellValueChanged:withIndex:)]) {
        [self.delegate inputCellValueChanged:textField.text withIndex:self.indexPath];
    }
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    NSLog(@"djfkdfjdkfjdkfjdkfjdkf");
    if ([self.delegate respondsToSelector:@selector(textFiledBegin:)]) {
        [self.delegate textFiledBegin:textField];
    }
}
#pragma mark - 创建cell
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString * ID = @"JSLTableBillEditCell";
    id cell = [tableView dequeueReusableCellWithIdentifier:ID ];
    
    if (cell == nil) {
        cell = [[self alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    
    return cell;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}


@end
